package abmb.pages;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import abmb.utils.MBPost;
import abmb.utils.PageLinks;
import abmb.utils.PageParts;
import abmb.utils.Utilities;

/**
 * Servlet implementation class PostMessage
 * @author Andrea Balboni
 * @version 1.0
 */
public class PostMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PostMessage() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		Utilities utils=new abmb.utils.Utilities();
		PageParts pp=new abmb.utils.PageParts();
		HttpSession session=request.getSession(true);
		boolean loggedin=utils.isLoggedIn(session);
		String pTitle=null,pBody=null,pTags[];
		pTags=new String[4];
		int pID=-1,userID=-1;
		String action="insert";
		MBPost post=new MBPost();
		if(!loggedin){
			response.sendRedirect(response.encodeRedirectURL(PageLinks.getIndexLink()));
		}
		response.setContentType("text/html");
		PrintWriter p= response.getWriter();
		p.println(pp.printHTMLHeader("AB-Microblog Post Message",loggedin));
		p.println(pp.printSidebar(loggedin));
		if(request.getParameter("action")!=null){
			action=(String)request.getParameter("action").toString();
		}
		if(request.getParameter("title")!=null)
			pTitle=(String)request.getParameter("title").toString();
		if(request.getParameter("threadbody")!=null)
			pBody=(String)request.getParameter("threadbody").toString();
		for(int i=1;i<=4;++i){
			if(request.getParameter("tag"+i)!=null){
				pTags[i-1]=(String)request.getParameter("tag"+i).toString();
			}
		}
		if(request.getParameter("postID")!=null){
			pID=Integer.parseInt((String)request.getParameter("postID").toString());
			post.postID(pID);
		}
		if(session.getAttribute("userID")!=null){
			userID=Integer.parseInt((String)session.getAttribute("userID").toString());
		}
		if(action.equals("delete")){
			if(!utils.doDeleteMessage(pID)){
				response.sendRedirect(response.encodeRedirectURL(PageLinks.getPostMessageLink()));
				p.println("Unable to post message");
			}else{
				response.sendRedirect(response.encodeRedirectURL(PageLinks.getIndexLink()));
			}
		}
		else{
			p.println(pp.printPostForm(pID, userID,action));
		}
		if(request.getParameter("title")!=null && request.getParameter("threadbody")!=null && session.getAttribute("userID")!=null){

			post.userID(Integer.parseInt((String)session.getAttribute("userID").toString()));
			post.title(pTitle);
			try {
				post.body(pBody);
			} catch (Exception e) {
				response.sendRedirect(response.encodeRedirectURL(PageLinks.getPostMessageLink()));
				p.println("Message body cannot be published, try again");
				e.printStackTrace();
			}
			post.setPostDate(new java.util.Date(System.currentTimeMillis()));

			
			post.author((String) request.getParameter("nickname"));
			for(String t:pTags){
				if(t!="" && t!=" "){
					post.tags().add(t);
				}
			}
			if(action.equals("insert")){
				if(!utils.doPostMessage(post)){
					response.sendRedirect(response.encodeRedirectURL(PageLinks.getPostMessageLink()));
					p.println("Unable to post message");
				}else{
					response.sendRedirect(response.encodeRedirectURL(PageLinks.getIndexLink()));
				}

			}
			if(action.equals("edit")){
				if(!utils.doEditMessage(post)){
					response.sendRedirect(response.encodeRedirectURL(PageLinks.getPostMessageLink()));
					p.println("Unable to post message");
				}else{
					response.sendRedirect(response.encodeRedirectURL(PageLinks.getIndexLink()));
				}

			}

		}
		p.println(pp.printFooterSection());
		p.println(pp.printHTMLFooter());

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
